package com.media.publish.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.media.R;
import com.media.publish.video_view.BaseBottomView;
import com.qiniu.pili.droid.shortvideo.PLMediaFile;

/**
 * 编辑模块贴纸相关的功能视图基类（文字特效、动态贴纸）
 */
public abstract class StickerBottomView extends BaseBottomView {

    protected Context mContext;
    protected AppCompatActivity mAct;
    protected LayoutInflater mInflater;
    protected RecyclerView mStickerRecyclerView;
    protected EditText mEdtVideoAddTextEdit;
    protected PLMediaFile mMediaFile;
    protected boolean mIsPlaying = true;

    protected OnViewOperateListener mOnViewOperateListener;
    protected ImageView mIvVideoAddTextClose;
    protected TextView mTvVideoAddTextConfirm;
    protected View mViewOccupy;

    public interface OnViewOperateListener {
        void onConfirmClicked();

        void onPlayStatusPaused();

        void onPlayProgressChanged(int position);

        void onCloseBottomView();
    }

    protected abstract void playStatusChanged(boolean isPlaying);

    public StickerBottomView(@NonNull Context context, AppCompatActivity act, PLMediaFile mediaFile, LayoutInflater mInflater) {
        super(context);
        mContext = context;
        mMediaFile = mediaFile;
        this.mInflater = mInflater;
        this.mAct = act;
        init();
    }

    public void setOnViewOperateListener(OnViewOperateListener listener) {
        mOnViewOperateListener = listener;
    }

    /**
     * 开始播放
     */
    public void startPlayer() {
        mIsPlaying = true;
    }

    @Override
    protected void init() {
        //文字布局
        final View view = LayoutInflater.from(mContext).inflate(R.layout.editor_gif_sticker_view, this);
        mIvVideoAddTextClose = view.findViewById(R.id.iv_videoaddText_close);
        mTvVideoAddTextConfirm = view.findViewById(R.id.tv_videoaddText_confirm);
        mEdtVideoAddTextEdit = view.findViewById(R.id.edt_videoaddText_edit);
        mStickerRecyclerView = view.findViewById(R.id.gif_sticker_list_view);
        mViewOccupy = view.findViewById(R.id.view_occupy);
        // 获取屏幕宽度
        WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;

        mTvVideoAddTextConfirm.setOnClickListener(v -> {
            if (mOnViewOperateListener != null) {
                mOnViewOperateListener.onConfirmClicked();
            }
        });
        mIvVideoAddTextClose.setOnClickListener(v -> {
            if (mOnViewOperateListener != null) {
                mOnViewOperateListener.onCloseBottomView();
            }
        });

        mEdtVideoAddTextEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    mTvVideoAddTextConfirm.setBackgroundResource(R.drawable.shape_next);
                    mTvVideoAddTextConfirm.setTextColor(Color.parseColor("#FFFFFF"));
                    mTvVideoAddTextConfirm.setEnabled(true);
                } else {
                    mTvVideoAddTextConfirm.setBackgroundResource(R.drawable.shape_next_2);
                    mTvVideoAddTextConfirm.setEnabled(false);
                    mTvVideoAddTextConfirm.setTextColor(Color.parseColor("#B3FFFFFF"));

                }
            }
        });
    }

    public void showSoftInputFromWindow(Activity activity) {
        if (mEdtVideoAddTextEdit != null) {
            mEdtVideoAddTextEdit.setFocusable(true);
            mEdtVideoAddTextEdit.setFocusableInTouchMode(true);
            mEdtVideoAddTextEdit.requestFocus();
            activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            InputMethodManager inputManager = (InputMethodManager) mEdtVideoAddTextEdit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(mEdtVideoAddTextEdit, 0);
        }
    }


    @Override
    public boolean isPlayerNeedZoom() {
        return false;
    }
}
